<script setup lang="ts">
import { ChatRound, Refresh, Search } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import { useUserFriendStore, useUserStore } from '/@/store'
import { storeToRefs } from 'pinia'

const tableRef = ref()
const loading = ref(false)
const searchKeyword = ref('')
const userFriendStore = useUserFriendStore()
const { chatGroup } = storeToRefs(userFriendStore)
const store = useUserStore()

const loadData = async () => {
  loading.value = true
  try {
    await userFriendStore.updateChatGroup()
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  loadData()
}

onMounted(() => {
  if (store.isSign) {
    loadData()
  }
})
</script>

<template>
  <div class="group-list-container">
    <div class="header">
      <h2 class="title">群组列表</h2>
      <div class="search-box">
        <el-input
          v-model="searchKeyword"
          placeholder="搜索群组"
          class="search-input"
          :prefix-icon="Search"
          @keyup.enter="handleSearch"
        />
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <div class="table-container">
      <el-button
        @click="loadData"
        class="refresh-btn"
        :loading="loading"
        type="primary"
        plain
      >
        <el-icon><Refresh /></el-icon>
        刷新列表
      </el-button>

      <div class="table-wrapper">
        <el-table
          :ref="tableRef"
          :data="chatGroup"
          style="width: 100%"
          v-loading="loading"
          :empty-text="'暂无群组'"
          border
          stripe
        >
          <el-table-column prop="name" label="群组名称" min-width="120">
            <template #default="{ row }">
              <div class="group-info">
                <el-avatar :size="40" :src="row.avatar || ''" />
                <div class="group-detail">
                  <span class="group-name">{{ row.name }}</span>
                  <span class="group-id" v-if="row.id">ID: {{ row.id }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="description"
            label="群组简介"
            min-width="200"
            show-overflow-tooltip
          />
          <el-table-column
            prop="memberCount"
            label="成员数"
            width="100"
            align="center"
          >
            <template #default="{ row }">
              <el-tag size="small" type="info">
                {{ row.memberCount || 0 }} 人
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="120">
            <template #default="{ row }">
              <el-button type="primary" link size="small" @click="() => {}">
                <el-icon><ChatRound /></el-icon>
                进入群组
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<style scoped>
.group-list-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.title {
  margin: 0;
  color: #303133;
  font-size: 24px;
}

.search-box {
  display: flex;
  gap: 10px;
}

.search-input {
  width: 300px;
}

.table-container {
  margin-top: 20px;
  height: calc(100vh - 200px);
  display: flex;
  flex-direction: column;
}

.table-wrapper {
  flex: 1;
  overflow: auto;
  margin-bottom: 20px;
}

.refresh-btn {
  margin-bottom: 16px;
}

.group-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.group-detail {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.group-name {
  font-weight: 500;
  color: #303133;
}

.group-id {
  font-size: 12px;
  color: #909399;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 600;
}

:deep(.el-table td) {
  padding: 12px 0;
}

@media screen and (max-width: 768px) {
  .header {
    flex-direction: column;
    gap: 16px;
  }

  .search-box {
    width: 100%;
  }

  .search-input {
    width: 100%;
  }
}
</style>
